package io.renren.modules.generator.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qcloud.cos.http.HttpRequest;

import io.renren.modules.generator.entity.StudentEntity;
import io.renren.modules.generator.service.StudentService;
import io.renren.util.UUIDUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-10-13 22:43:30
 */
@RestController
@RequestMapping("/generator/student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	
	public R findAllStudent(){
		List<StudentEntity> studentList = studentService.findAllStudent();
		return R.ok().put("page", studentList);
	}
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:student:list")
	public R list(@RequestParam Map<String, Object> params){
		String commonQuery = null != params.get("commonQuery")?params.get("commonQuery").toString():null;
		if(null != commonQuery && !"".equals(commonQuery)){
			//查询列表数据
	        Query query = new Query(params);

			List<StudentEntity> studentList = studentService.commonQueryQueryList(query);
			int total = studentService.queryTotal(query);
			
			PageUtils pageUtil = new PageUtils(studentList, total, query.getLimit(), query.getPage());
			
			return R.ok().put("page", pageUtil);
		}else{
			//查询列表数据
	        Query query = new Query(params);

			List<StudentEntity> studentList = studentService.queryList(query);
			int total = studentService.queryTotal(query);
			
			PageUtils pageUtil = new PageUtils(studentList, total, query.getLimit(), query.getPage());
			
			return R.ok().put("page", pageUtil);
		}
		
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{studentno}")
	@RequiresPermissions("generator:student:info")
	public R info(@PathVariable("studentno") String studentno){
		StudentEntity student = studentService.queryObject(studentno);
		
		return R.ok().put("student", student);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("generator:student:save")
	public R save(@RequestBody StudentEntity student){
		studentService.save(student);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("generator:student:update")
	public R update(@RequestBody StudentEntity student){
		studentService.update(student);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:student:delete")
	public R delete(@RequestBody String[] studentnos){
		studentService.deleteBatch(studentnos);
		
		return R.ok();
	}
	
	/**
	 * 退学
	 * @param studentnos
	 * @return
	 */
	@RequestMapping("/dropout")
	@RequiresPermissions("generator:student:dropout")
	public R dropout(@RequestBody String[] studentnos){
		boolean flag = studentService.dropout(studentnos);
		if(flag){
			return R.ok();
		}else{
			return R.error("学生退学失败");
		}
		
	}
	
	/**
	 * 学员报名
	 * @param studentnos
	 * @return
	 */
	@RequestMapping("/singup")
	@RequiresPermissions("generator:student:singup")
	public R singUp(@RequestBody String[] studentnos){
		boolean flag = studentService.dropout(studentnos);
		if(flag){
			return R.ok();
		}else{
			return R.error("学员报名失败");
		}
		
	}
}
